package com.tywj.panda.home

import android.graphics.Rect
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.tywj.panda.customer.common.tool.MoneyTool
import com.tywj.panda.customer.common.widget.recyclerview.MyBaseAdapter
import com.tywj.panda.home.entity.HomeLineEntity
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.core.widget.recycleview.adapter.BaseViewHolder


/**
 * @desc 线路适配器
 * @author June Yang
 * @date 2020/5/18
 *
 */
class HomeLineAdapter : MyBaseAdapter<HomeLineEntity.DataBean>(R.layout.home_recycle_item_line) {

    override fun convert(helper: BaseViewHolder, item: HomeLineEntity.DataBean) {
        super.convert(helper, item)

        helper.setText(R.id.tvLineBusName, item.name)
        helper.setText(R.id.tvStartAndEnd, item.fiedName)

        helper.setText(R.id.tvNote, item.note)

        helper.setGone(R.id.viewTips, item.note.isNotEmpty())

        helper.setText(R.id.tvOrderPrice, MoneyTool.formatFen(item.price))

        getType(helper, item.type)
        helper.addOnClickListener(R.id.btnBuy)
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {

                val position = parent.getChildLayoutPosition(view)
                if (position != RecyclerView.NO_POSITION) {
                    when (position) {
                        // 第一个
                        0 -> {
                            outRect.top = ResourcesUtil.getDimensionPixelSize(R.dimen.dp_10)
                            outRect.bottom = ResourcesUtil.getDimensionPixelSize(R.dimen.dp_10)
                        }
                        // footer
                        this@HomeLineAdapter.data.size + this@HomeLineAdapter.headerLayoutCount -> outRect.top = 0
                        // 其他
                        else -> outRect.bottom = ResourcesUtil.getDimensionPixelSize(R.dimen.dp_10)
                    }
                }
            }
        })
    }

    private fun getType(helper: BaseViewHolder, type: Int) {
        val tvStatus = helper.getView<TextView>(R.id.tvStatus)
        when (type) {
            1 -> {
                tvStatus.setBackgroundColor(
                    ResourcesUtil.getColor(
                        R.color.common_green_54
                    )
                )
                helper.setText(R.id.tvStatus, "常用")
            }
            2 -> {
                tvStatus.setBackgroundColor(
                    ResourcesUtil.getColor(
                        R.color.common_yellow_FD
                    )
                )
                helper.setText(R.id.tvStatus, "推荐")
            }
            else -> {
                tvStatus.setBackgroundColor(ResourcesUtil.getColor(R.color.common_transparent))
            }
        }
    }
}